class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        int n = arr.size();
        if (n == 1) return 1;
        vector<int> dp(n, 1);
        unordered_map<int, int> hash;
        hash[arr[0]] = dp[0];
        int ret = 0;
        for (int i = 1;i < n;i++)
        {
            unordered_map<int, int>::iterator it = hash.find(arr[i] - difference);
            if (it != hash.end())
            {
                dp[i] = max(dp[i], it->second + 1);
                cout << hash[arr[i]] << " ";
            }
            hash[arr[i]] = dp[i];
            ret = max(ret, dp[i]);
        }
        cout << endl;
        for (int i = 0;i < n;i++)
        {
            cout << dp[i] << " ";
        }
        return ret;
    }
};